package de.grosjohann.android.feuerwehr.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import de.grosjohann.android.feuerwehr.R;
import de.grosjohann.android.feuerwehr.domain.WeatherWarning;
import de.grosjohann.android.feuerwehr.type.WeatherWarningType;
import de.grosjohann.android.feuerwehr.util.ColorUtil;

public class WeatherListAdapter
  extends BaseAdapter {

  private static final String NAME = "WeatherListAdapter";

  private static final List<WeatherWarning> list = new ArrayList<WeatherWarning>();

  private final Activity context;
  private final int listViewId;
  private final boolean attachToRoot;

  public WeatherListAdapter(Activity ctx, int viewId) {
    this(ctx, viewId, null, false);
  }

  public WeatherListAdapter(Activity ctx, int viewId, List<WeatherWarning> newList) {
    this(ctx, viewId, newList, false);
  }

  public WeatherListAdapter(Activity ctx, int viewId, List<WeatherWarning> newList, boolean attachViewToRoot) {
    context = ctx;
    listViewId = viewId;
    list.clear();
    if (newList != null) {
      list.addAll(newList);
    }
    attachToRoot = attachViewToRoot;
  }

  @Override
  public int getCount() {
    return list.size();
  }

  @Override
  public WeatherWarning getItem(int i) {
    return list.get(i);
  }

  @Override
  public long getItemId(int i) {
    return i;
  }

  @Override
  public boolean isEnabled(int i) {
    return false;
  }

  @Override
  public boolean areAllItemsEnabled() {
    return false;
  }

  @Override
  public View getView(int index, View convertView, ViewGroup parent) {
    View row = null;
    WeatherWarning ww = list.get(index);

    if (convertView == null) {
      row = LayoutInflater.from(context).inflate(listViewId, parent, attachToRoot);
    } else {
      row = convertView;
    }

    TextView tvWarningMessage = (TextView) row.findViewById(R.id.tvWarningMsg);
    tvWarningMessage.setText(ww.getMessage());

    WeatherWarningType level = ww.getLevel();
    TextView tvWarningLevel = (TextView) row.findViewById(R.id.tvWarningLevel);
    tvWarningLevel.setText(level.getResId());

    TextView tvWarningLevelMarker = (TextView) row.findViewById(R.id.tvWarningLevelMarker);
    tvWarningLevelMarker.setBackgroundColor(ww.getColor());
    row.setBackgroundColor(ColorUtil.getAlphaColor(136, ww.getColor()));

    TextView tvWarningEnd = (TextView) row.findViewById(R.id.tvWarningEnd);
    if (ww.getEnd() == null) {
      tvWarningEnd.setVisibility(View.GONE);
    } else {
      String msg = context.getString(R.string.lblWarningEndPrefix) + " " + ww.getEnd();
      tvWarningEnd.setVisibility(View.VISIBLE);
      tvWarningEnd.setText(msg);
    }

    return row;
  }

  public void addAll(List<WeatherWarning> newList) {
    list.clear();
    list.addAll(newList);

    Log.d(NAME, list.toString());

    super.notifyDataSetChanged();
  }
}
